import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        Authorization: localStorage.getItem('Authorization') ? localStorage.getItem('Authorization') : '',
        UserInfo: localStorage.getItem('UserInfo') ? localStorage.getItem('UserInfo') : '',
        CurPageTitle: '',
        CurProject: localStorage.getItem('CurProject') ? JSON.parse(localStorage.getItem('CurProject')) : {},
    },
    mutations: {
        setCurPageTitle(state, title) {
            state.CurPageTitle = title;
        },
        setCurProject(state, project) {
            state.CurProject = project;
            localStorage.setItem("CurProject", JSON.stringify(project));
        },
        // 存储token
        setToken(state, userInfo) {
            const token = userInfo.authorization;
            state.Authorization = token;
            localStorage.setItem("Authorization", token);

            state.UserInfo = userInfo;
            localStorage.setItem("UserInfo", JSON.stringify(userInfo));
        },
        //删除Token
        delToken(state) {
            state.Authorization = '';
            localStorage.removeItem("Authorization");

            state.UserInfo = '';
            localStorage.removeItem("UserInfo");
        },
        delCurProject(state){
            state.CurProject = {};
            localStorage.removeItem("CurProject");
        }
    }
})

export default store;
